﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DTO;
namespace DAO
{
    public class ChiTietLSVayDA
    {
        public static ChiTietLSVay GetByID(int maID)
        {
            ChiTietLSVay loai = null;
            SqlConnection cn = DataProvider.Connect();
            SqlCommand cmd = new SqlCommand("Proc_ChiTietLSVay_GetById", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@maLSVay", maID));
            SqlDataReader dataReader = cmd.ExecuteReader();
            if (dataReader.Read())
            {
                loai = new ChiTietLSVay();
                loai.MaLSVay = maID;
                loai.MaKH = Convert.ToInt32(dataReader["MaKH"]);
                loai.MaNgayVDLS = Convert.ToInt32(dataReader["MaNgayVDLS"]);
                loai.MucLS = Convert.ToSingle(dataReader["MucLaiSuat"]);
                loai.LastUpdate = Convert.ToDateTime(dataReader["LastUpdate"]);

                loai.NgayVD = NgayVDLSDA.GetByID(loai.MaNgayVDLS);
                loai.DSKyHan = LoaiKyHanLSDA.GetByID(loai.MaKH);
            }
            cn.Close();
            return loai;
        }
        public static List<ChiTietLSVay> GetByMaNgayVDLS(int maID)
        {
            List<ChiTietLSVay> lst = new List<ChiTietLSVay>();
            ChiTietLSVay loai = null;
            SqlConnection cn = DataProvider.Connect();
            SqlCommand cmd = new SqlCommand("Proc_ChiTietLSVay__GetByMaNgayVDLS", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@maNgayVDLS", maID));
            SqlDataReader dataReader = cmd.ExecuteReader();
            while (dataReader.Read())
            {
                loai = new ChiTietLSVay();
                loai.MaNgayVDLS = maID;
                loai.MaKH = Convert.ToInt32(dataReader["MaKH"]);
                loai.MaLSVay = Convert.ToInt32(dataReader["MaLSVay"]);
                loai.MucLS = Convert.ToSingle(dataReader["MucLS"]);
                loai.LastUpdate = Convert.ToDateTime(dataReader["LastUpdate"]);

                loai.NgayVD = NgayVDLSDA.GetByID(loai.MaNgayVDLS);
                loai.DSKyHan = LoaiKyHanLSDA.GetByID(loai.MaKH);
                lst.Add(loai);
            }
            cn.Close();
            return lst;
        }
        public static List<ChiTietLSVay> GetByMaKH(int maID)
        {
            List<ChiTietLSVay> lst = new List<ChiTietLSVay>();
            ChiTietLSVay loai = null;
            SqlConnection cn = DataProvider.Connect();
            SqlCommand cmd = new SqlCommand("Proc_ChiTietLSVay__GetByMaKH", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@maNgayKH", maID));
            SqlDataReader dataReader = cmd.ExecuteReader();
            while (dataReader.Read())
            {
                loai = new ChiTietLSVay();
                loai.MaKH = maID;
                loai.MaNgayVDLS = Convert.ToInt32(dataReader["MaNgayVDLS"]);
                loai.MaLSVay = Convert.ToInt32(dataReader["MaLSVay"]);
                loai.MucLS = Convert.ToSingle(dataReader["MucLS"]);
                loai.LastUpdate = Convert.ToDateTime(dataReader["LastUpdate"]);

                loai.NgayVD = NgayVDLSDA.GetByID(loai.MaNgayVDLS);
                loai.DSKyHan = LoaiKyHanLSDA.GetByID(loai.MaKH);
                lst.Add(loai);
            }
            cn.Close();
            return lst;
        }
    }
}
